package ua.kpi_java_training.evgeniygusar.module3;

import java.util.Random;

public abstract class InhabitantOfTheDesert {
    public int x, y;
    int X_BORDER, Y_BORDER;
    public enum Destination {LEFT,right,up_left,up_right,down_left,down_right}

    InhabitantOfTheDesert() {

        Desert mydesert = new Desert();
        X_BORDER = mydesert.getXBorder();
        Y_BORDER = mydesert.getYBorder();
        Borning();
    }

    /**
     * Change x or y coordinate of InhabitantOfTheDesert due to destination, that generated at random in previous method
     * @param d - destination
     */
    AllObstacles obstaclesObject = new AllObstacles();

    /**
     * Algorithm of moving of each inhabitant of desert
     * @param d - destinaion of them. Can be: left, right, up_left, up_right, down_left, down_right
     */
    public void Move(Destination d) {
        switch (d) {
            case LEFT:
                if((x!=1)&(!obstaclesObject.checkObstacleInXY(x-1,y)))
                    x--;
                break;
            case right:
                if((x!=X_BORDER)&(!obstaclesObject.checkObstacleInXY(x+1,y)))
                    x++;
                break;
            case up_left:
                if((y%2!=0)&(y!=1)&(x!=1)&(!obstaclesObject.checkObstacleInXY(x-1,y-1))) {
                    y--;
                    x--;
                }
                else if((y%2==0)&(y!=1)&(!obstaclesObject.checkObstacleInXY(x,y-1)))
                    y--;
                break;
            case up_right:
                if((y%2!=0)&(y!=1)&(x!=X_BORDER)&(!obstaclesObject.checkObstacleInXY(x,y-1)))
                    y--;
                else if((y%2==0)&(y!=1)&(!obstaclesObject.checkObstacleInXY(x+1,y-1))) {
                    y--;
                    x++;
                }
                break;
            case down_left:
                if((y%2!=0)&(y!=Y_BORDER)&(x!=1)&(!obstaclesObject.checkObstacleInXY(x-1,y+1))) {
                    y++;
                    x--;
                }
                else if((y%2==0)&(y!=Y_BORDER)&(!obstaclesObject.checkObstacleInXY(x,y+1)))
                    y++;
                break;
            case down_right:
                if((y%2!=0)&(y!=Y_BORDER)&(x!=X_BORDER)&(!obstaclesObject.checkObstacleInXY(x,y+1)))
                    y++;
                else if((y%2==0)&(y!=Y_BORDER)&(!obstaclesObject.checkObstacleInXY(x+1,y+1))) {
                    y++;
                    x++;
                }
                break;
        }
    }


    /**
     * Create random coordinats for new InhabitantOfTheDesert
     */
    AllObstacles obstaclesObject2 = new AllObstacles();

    /**
     * Creationg x and y coordinats for each new inhabitant of desert
     */

    public void Borning() {
        do {
        Random rnd_x = new Random(System.currentTimeMillis());
        x=1+rnd_x.nextInt(X_BORDER);
        Random rnd_y = new Random(System.currentTimeMillis()+1);
        y=1+rnd_y.nextInt(Y_BORDER);
        }
        while(obstaclesObject.checkObstacleInXY(x,y));
    }

    /**
     * Output of new inhabitant of desert coordinats
     */
    public void showCoordinats() {
        System.out.println("x = " + x + ", y = " + y);
    }
}

